package com.iris.getTableVO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.iris.utils.DataDict;
import com.iris.utils.JDBCTemplate;

public class GetTableVO2 {
	
	public void printMysqlString(String table){
		JDBCTemplate template=new JDBCTemplate();
		Connection conn=template.getConnect();
		String sql="";
		String column="";
		String type="String";
		try {
			PreparedStatement ps=conn.prepareStatement("select * from "+table+" where 1=2");
			ResultSet rs=ps.executeQuery();
			ResultSetMetaData rsmd=rs.getMetaData();
			String schema = JDBCTemplate.getPro().getProperty(DataDict.JdbcKey.URL.getValue()+"."+DataDict.DataType.MYSQL);
			schema=schema.substring(schema.lastIndexOf("/")+1,schema.length());
			sql="select * from information_schema.columns where table_name='"+table+"' and table_schema='"+schema+"'";
//			System.out.println(sql);
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				for (int i = 1; i <= rsmd.getColumnCount(); i++) {
					column=changeColumn(rsmd.getColumnName(i).toLowerCase());
					type = DataDict.getMysqlColumnMapping(rsmd.getColumnTypeName(i));
					if(rs.getString("column_name").toLowerCase().equals(rsmd.getColumnName(i).toLowerCase())){
						System.out.println("private "+type+" "+column+" ;//"+rs.getString("COLUMN_COMMENT"));
					}
				}
			}
			//打印setString
//			for (int i = 1; i <= rsmd.getColumnCount(); i++) {
//				column=rsmd.getColumnName(i).toLowerCase();
//				column = changeColumn(column);
//				column = createGetSetString(column, DataDict.getMysqlColumnMapping(rsmd.getColumnTypeName(i)));
//				System.out.println(column);
//			}
			System.out.println("======================");
			rs.beforeFirst();
			//打印hibernate
			while(rs.next()){
				column = rs.getString("column_name").toLowerCase();
				type = DataDict.getMysqlHibernateMapping(rs.getString("data_type").toUpperCase());
				System.out.println(createMybatisString(column, type,rs.getString("CHARACTER_MAXIMUM_LENGTH"),rs.getString("COLUMN_COMMENT")));
			}

			System.out.println("======================");
			
			createMybatisUpdateInsertString(rs,table);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	
	public void createMybatisUpdateInsertString(ResultSet rs,String tableName){
		try {
			rs.beforeFirst();
			//打印mybatis 的update 和insert 语句
			String column ="",proCol="";
			System.out.println("insert into "+tableName+"\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >");
			String insertValues="</trim>\n<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >";
			String udpateSql="update "+tableName+"\n<set>";
			String columns="";
			while(rs.next()){
				column = rs.getString("column_name").toLowerCase();
				columns+=column+",";
//				type = DataDict.getMysqlHibernateMapping(rs.getString("data_type").toUpperCase());
				proCol = changeColumn(column);
				System.out.println("\t<if test=\""+proCol+" != null\" >\n\t\t"+column+",\n\t</if>");
				insertValues+="\n\t<if test=\""+proCol+" != null\" >\n\t\t#{"+proCol+"},\n\t</if>";
				udpateSql+="\n\t<if test=\""+proCol+" != null\" >\n\t\t"+column+" = #{"+proCol+"},\n\t</if>";
			}
			insertValues+="\n</trim>";
			udpateSql+="\n</set>";
			System.out.println(insertValues);
			System.out.println("</resultMap>");
			System.out.println("");
			System.out.println("");
			System.out.println(udpateSql);
			System.out.println(columns);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String createGetSetString(String str,String type){
		StringBuilder result=new StringBuilder();
		String start=str.substring(0,1).toUpperCase();
		String end=str.substring(1,str.length());
		String methodName=start+end;
		result.append("public ").append(type).append(" get")
		.append(methodName).append("(){\n\t").append("return ").append(str).append(";\n")
		.append("}\n");
		result.append("public void ").append("set")
		.append(methodName).append("(").append(type).append(" ").append(str).append("){\n\t").append("this.").append(str).append(" = ")
		.append(str).append(";\n")
		.append("}\n");
		return result.toString();
	}

	

	public String createMybatisString(String str,String type,String length,String comment){
		StringBuilder result=new StringBuilder();
		result.append("<result column=\"").append(str).append("\" property=\"").append(changeColumn(str)).append("\" />");
		return result.toString();
	}
	
	public String changeColumn(String column){
		column = column.toLowerCase();
		Pattern pattern = Pattern.compile("_[a-zA-Z0-9]");  
        Matcher matcher=pattern.matcher(column); 
        while (matcher.find()) {
        	column = column.replaceAll( matcher.group(),matcher.group().toUpperCase().replaceAll("_", ""));
        }
		return column;
	}
	public static void main(String[] args) {
		GetTableVO2 gss=new GetTableVO2();
		gss.printMysqlString("zed_gbox_account" );
	}
}
